package org.hhchat.leetcode.tree;

import org.hhchat.leetcode.dataStruct.TreeNode;

/**
 * Created this one by HMH on 2017/10/1.
 */
public class code681_easy {

    static class Solution {

        int max = 0;
        public int longestUnivaluePath(TreeNode root) {
            findSubTree(root);
            return max;
        }

        public int findSubTree(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int left = 0;
            int right = 0;
            if (root.left != null) {
                left = findSubTree(root.left);
            }
            if (root.right != null) {
                right = findSubTree(root.right);
            }
            if (root.left!=null && root.left.val == root.val) {
                left = left + 1;
            }else {
                left = 0;
            }
            if (root.right!=null && root.right.val == root.val) {
                right = right + 1;
            }else{
                right = 0;
            }
            max = Integer.max(max, left + right);
            return Integer.max(left, right);
        }


    }

    public static void main(String[] args){

    }

}
